<?php
/**
 * Created by Command.
 * User: Vito
 */

declare (strict_types = 1);

namespace app\model;

class RoomLeaseRecord extends BaseModel
{

    protected $name = 'RoomLeaseRecord';
    const SEX_UNKNOWN = 0;
    const SEX_MALE = 1;
    const SEX_FEMALE = 2;

    protected $sexEnum = [
        self::SEX_UNKNOWN => '未知',
        self::SEX_MALE    => '男',
        self::SEX_FEMALE  => '女',
    ];

    const ALONE_NO = 0;
    const ALONE_YES = 1;

    protected $aloneEnum = [
        self::ALONE_NO   => '合租',
        self::ALONE_YES  => '独居'
    ];

    const STATUS_ING = 0;
    const STATUS_ED = 1;
    const STATUS_OUT = 2;
    const STATUS_BACK = 3;

    protected $statusEnum = [
        self::STATUS_ING    => '待审核',
        self::STATUS_ED     => '居住中',
        self::STATUS_OUT    => '已退房',
        self::STATUS_BACK   => '驳回'
    ];
    /**
     * @Description: 关联用户信息表
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-10-25 10:24:37
     * @version: 1.0.0
     * @return [type]
     */
    public function user(){

        return $this->belongsTo('user','user_id')->bind([
            'sex' => 'sex',
            'real_name' => 'real_name'
        ]);
    }
    /**
     * @Description: 修改入住申请记录数据
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where     查询条件
     * @param [type] $data      更新数据
     * @Date: 2022-10-26 15:47:18
     * @version: 1.0.0
     * @return [type]
     */    
    public static function updateRecord($where,$data){

        return self::where($where)->update($data);
    }
    /**
     * @Description: 获取入住记录
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where
     * @param [type] $data
     * @Date: 2022-10-27 17:36:30
     * @version: 1.0.0
     * @return [type]
     */
    public static function getUserRecord($where){

        return self::where($where)->column('user_id');
    }
    /**
     * @Description: 关联房间
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-19 14:26:41
     * @version: 1.0.0
     * @return [type]
     */    
    public function room(){
        return $this->belongsTo('room','room_id')->with(['roomType', 'building']);
    }
    /**
     * @Description: 关联房型
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-19 14:32:36
     * @version: 1.0.0
     * @return [type]
     */    
    public function roomType(){
        return $this->hasOneThrough('roomType','room', 'id', 'id', 'room_id', 'room_type');
    }
    /**
     * @Description: 关联楼房
     * @author: xiayu <cb1071403016@126.com>
     * @Date: 2022-11-19 14:32:36
     * @version: 1.0.0
     * @return [type]
     */
    public function building()
    {
        return $this->hasOneThrough('building', 'room', 'id', 'id', 'room_id', 'building_number');
    }
    // 关联缴费记录
    public function payRecord()
    {
        return $this->hasOne('PayRecordCopy', 'user_id', 'user_id')->order(['id' => 'desc']);
    }
    /**
     * @Description: 获取用户申请记录
     * @author: xiayu <cb1071403016@126.com>
     * @param [type] $where
     * @Date: 2022-11-09 10:51:12
     * @version: 1.0.0
     * @return [type]
     */
    public static function getRecord($where){
        $res = self::where($where)->order('id', 'desc')->limit(10)->with(['room','user','building'])->select();
        // $result = $res->user;
        return $res;
    }

    public static  function getTenantList($params = null)
    {
        $where['status'] = 1;
        if(!empty($params)){
            $where['id'] = $params['id'];
        }
        $res = self::where($where)->order(['id' => 'desc'])->with(['user','room', 'payRecord'])->select();
        return $res;
    }

    // 修改入住时间
    public function getStartTimeAttr($value){
        return date("Y-m-d",$value);
    }
}
